package org.zwy.gradetrack.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.zwy.gradetrack.common.Result;
import org.zwy.gradetrack.entity.GraduationRequirement;
import org.zwy.gradetrack.service.GraduationRequirementService;

import java.util.List;

/**
 * 毕业要求管理控制器
 */
@RestController
@RequestMapping("/api/requirements")
public class GraduationRequirementController {

    @Autowired
    private GraduationRequirementService graduationRequirementService;

    /**
     * 获取毕业要求列表
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER','GRADUATE')")
    public Result<List<GraduationRequirement>> getRequirementList(@RequestParam(required = false) Long majorId) {
        List<GraduationRequirement> requirements;
        if (majorId != null) {
            requirements = graduationRequirementService.lambdaQuery()
                    .eq(GraduationRequirement::getMajorId, majorId)
                    .list();
        } else {
            requirements = graduationRequirementService.list();
        }
        return Result.success(requirements);
    }

    /**
     * 创建毕业要求
     */
    @PostMapping
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<GraduationRequirement> createRequirement(@RequestBody GraduationRequirement requirement) {
        // 检查毕业要求代码是否已存在
        if (graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getRequirementCode, requirement.getRequirementCode())
                .exists()) {
            return Result.error(400, "毕业要求代码已存在");
        }

        // 保存毕业要求信息
        graduationRequirementService.save(requirement);
        return Result.success(requirement);
    }

    /**
     * 更新毕业要求
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<GraduationRequirement> updateRequirement(@PathVariable Long id, @RequestBody GraduationRequirement requirement) {
        // 检查毕业要求是否存在
        if (!graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getId, id)
                .exists()) {
            return Result.error(404, "毕业要求不存在");
        }

        // 检查毕业要求代码是否重复
        if (graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getRequirementCode, requirement.getRequirementCode())
                .ne(GraduationRequirement::getId, id)
                .exists()) {
            return Result.error(400, "毕业要求代码已存在");
        }

        // 更新毕业要求信息
        requirement.setId(id);
        graduationRequirementService.updateById(requirement);
        return Result.success(requirement);
    }

    /**
     * 删除毕业要求
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('SYSTEM_ADMIN')")
    public Result<Void> deleteRequirement(@PathVariable Long id) {
        // 检查毕业要求是否存在
        if (!graduationRequirementService.lambdaQuery()
                .eq(GraduationRequirement::getId, id)
                .exists()) {
            return Result.error(404, "毕业要求不存在");
        }

        // 删除毕业要求
        graduationRequirementService.removeById(id);
        return Result.success();
    }

    /**
     * 获取毕业要求详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('SYSTEM_ADMIN', 'TEACHER')")
    public Result<GraduationRequirement> getRequirementDetail(@PathVariable Long id) {
        GraduationRequirement requirement = graduationRequirementService.getById(id);
        if (requirement == null) {
            return Result.error(404, "毕业要求不存在");
        }
        return Result.success(requirement);
    }
} 